﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TaxAssistantBase;

namespace ShiBieProject
{
    public class VerificationVendorsOrCustomer
    {
        public static void VerificationVendorsOrCustomerCompany(List<InvoiiceOcrModel> idIOMList) {
            List<BatchInfo> companynamelsit = new List<BatchInfo>();
            List<BatchInfo> companytaxnolist = new List<BatchInfo>();
            List<BatchInfo> VendorscompanyAll =  HongFengCompanyOrVendorsTool.VendorscompanyAll;
            int tiaoNim = 0;
            foreach (InvoiiceOcrModel iom in idIOMList) {
                tiaoNim++;
                if (iom.companyState == "1")
                {
                    continue;
                }
                try
                {
                    if (iom.invoiceOcrSonLists != null && iom.invoiceOcrSonLists.Count > 0)
                    {
                        string gfcompanyName = iom.invoiceOcrSonLists[0].gfcompanyname;
                        string gftaxnumber = iom.invoiceOcrSonLists[0].gftaxnumber;
                        string xfcompanyname = iom.invoiceOcrSonLists[0].xfcompanyname;
                        string xftaxnumber = iom.invoiceOcrSonLists[0].xftaxnumber;
                        //销项比对购方公司
                        if (iom.invoiceOcrSonLists[0].isProviderOrCustomer == "0")
                        {
                            var gfcompanytaxnolist = from gf in HongFengCompanyOrVendorsTool.VendorscompanyAll where gf.companyTaxnum == gftaxnumber select gf;
                            var gfcompanynamelist = from gf in HongFengCompanyOrVendorsTool.VendorscompanyAll where gf.companyName == gfcompanyName select gf;
                            companynamelsit = gfcompanynamelist.ToList();
                            companytaxnolist = gfcompanytaxnolist.ToList();
                            if (companynamelsit.Count > 0)
                            {
                                iom.invoiceOcrSonLists[0].gfcompanyname = companynamelsit[0].companyName;
                                iom.invoiceOcrSonLists[0].gfcompanynameInitialValue = companynamelsit[0].companyName;
                                iom.invoiceOcrSonLists[0].gftaxnumber = companynamelsit[0].companyTaxnum;
                                iom.invoiceOcrSonLists[0].gftaxnumberInitialValue = companynamelsit[0].companyTaxnum;
                                continue;
                            }
                            else if (companytaxnolist.Count > 0)
                            {
                                iom.invoiceOcrSonLists[0].gfcompanyname = companytaxnolist[0].companyName;
                                iom.invoiceOcrSonLists[0].gfcompanynameInitialValue = companytaxnolist[0].companyName;
                                iom.invoiceOcrSonLists[0].gftaxnumber = companytaxnolist[0].companyTaxnum;
                                iom.invoiceOcrSonLists[0].gftaxnumberInitialValue = companytaxnolist[0].companyTaxnum;
                                continue;
                            }
                            else if (gftaxnumber.Length > 6)
                            {
                                string gfnum = gftaxnumber.Substring(gftaxnumber.Length - 6, 6);
                                foreach (BatchInfo bf in VendorscompanyAll)
                                {
                                    if (bf.companyTaxnum.Length > 6)
                                    {
                                        string comtax = bf.companyTaxnum.Substring(bf.companyTaxnum.Length - 6, 6);
                                        if (gfnum == comtax)
                                        {
                                            iom.invoiceOcrSonLists[0].gfcompanyname = bf.companyName;
                                            iom.invoiceOcrSonLists[0].gfcompanynameInitialValue = bf.companyName;
                                            iom.invoiceOcrSonLists[0].gftaxnumber = bf.companyTaxnum;
                                            iom.invoiceOcrSonLists[0].gftaxnumberInitialValue = bf.companyTaxnum;
                                            break;
                                        }
                                    }
                                }
                            }
                            else
                            {//9131011509180874
                                if (gftaxnumber.Length>16) {
                                    string gftax = gftaxnumber.Substring(0, 16);
                                    foreach (BatchInfo bf in VendorscompanyAll)
                                    {
                                        if (bf.companyTaxnum.Length > 16)
                                        {
                                            string comtax = bf.companyTaxnum.Substring(0, 16);
                                            if (gftax == comtax)
                                            {
                                                iom.invoiceOcrSonLists[0].gfcompanyname = bf.companyName;
                                                iom.invoiceOcrSonLists[0].gfcompanynameInitialValue = bf.companyName;
                                                iom.invoiceOcrSonLists[0].gftaxnumber = bf.companyTaxnum;
                                                iom.invoiceOcrSonLists[0].gftaxnumberInitialValue = bf.companyTaxnum;
                                                break;
                                            }
                                        }
                                    }
                                }
                                else if(gftaxnumber.Length > 14)
                                {
                                    string gftaxmin = gftaxnumber.Substring(0, 14);
                                    foreach (BatchInfo bf in VendorscompanyAll)
                                    {
                                        if (bf.companyTaxnum.Length > 14)
                                        {
                                            string comtax = bf.companyTaxnum.Substring(0, 14);
                                            if (gftaxmin == comtax)
                                            {
                                                iom.invoiceOcrSonLists[0].gfcompanyname = bf.companyName;
                                                iom.invoiceOcrSonLists[0].gfcompanynameInitialValue = bf.companyName;
                                                iom.invoiceOcrSonLists[0].gftaxnumber = bf.companyTaxnum;
                                                iom.invoiceOcrSonLists[0].gftaxnumberInitialValue = bf.companyTaxnum;
                                                break; 
                                            }
                                        }
                                    }
                                }
                            }
                            string gfname = iom.invoiceOcrSonLists[0].gfcompanyname;
                            gfname = gfname.Replace('(', '（');
                            gfname = gfname.Replace(')', '）');
                            iom.invoiceOcrSonLists[0].gfcompanyname = gfname;
                            iom.invoiceOcrSonLists[0].gfcompanynameInitialValue = gfname;
                            iom.invoiceOcrSonLists[0].gfcompanynameState = CompanyOrMoneyStateEnum.InvoiceVendorsOrCustmIncorrect;
                        }
                        else if (iom.invoiceOcrSonLists[0].isProviderOrCustomer == "1")
                        {//进项票比对销方公司
                            var xfcompanytaxnolist = from xf in HongFengCompanyOrVendorsTool.VendorscompanyAll where xf.companyTaxnum == xftaxnumber select xf;
                            var xfcompanynamelist = from xf in HongFengCompanyOrVendorsTool.VendorscompanyAll where xf.companyName == xfcompanyname select xf;
                            companynamelsit = xfcompanynamelist.ToList();
                            companytaxnolist = xfcompanytaxnolist.ToList();
                            if (companynamelsit.Count > 0)
                            {
                                iom.invoiceOcrSonLists[0].xfcompanyname = companynamelsit[0].companyName;
                                iom.invoiceOcrSonLists[0].xfcompanynameInitialValue = companynamelsit[0].companyName;
                                iom.invoiceOcrSonLists[0].xftaxnumber = companynamelsit[0].companyTaxnum;
                                iom.invoiceOcrSonLists[0].xftaxnumberInitialValue = companynamelsit[0].companyTaxnum;
                                continue;
                            }
                            else if (companytaxnolist.Count > 0)
                            {
                                iom.invoiceOcrSonLists[0].xfcompanyname = companytaxnolist[0].companyName;
                                iom.invoiceOcrSonLists[0].xfcompanynameInitialValue = companytaxnolist[0].companyName;
                                iom.invoiceOcrSonLists[0].xftaxnumber = companytaxnolist[0].companyTaxnum;
                                iom.invoiceOcrSonLists[0].xftaxnumberInitialValue = companytaxnolist[0].companyTaxnum;
                                continue;
                            }
                            else if (xftaxnumber.Length > 6)
                            {
                                string xfnum = xftaxnumber.Substring(xftaxnumber.Length - 6, 6);
                                foreach (BatchInfo bf in VendorscompanyAll)
                                {
                                    if (bf.companyTaxnum.Length > 6)
                                    {
                                        string comtax = bf.companyTaxnum.Substring(bf.companyTaxnum.Length - 6, 6);
                                        if (xfnum == comtax)
                                        {
                                            iom.invoiceOcrSonLists[0].xfcompanyname = bf.companyName;
                                            iom.invoiceOcrSonLists[0].xfcompanynameInitialValue = bf.companyName;
                                            iom.invoiceOcrSonLists[0].xftaxnumber = bf.companyTaxnum;
                                            iom.invoiceOcrSonLists[0].xftaxnumberInitialValue = bf.companyTaxnum;
                                            break;
                                        }
                                    }
                                }
                            }
                            else {
                                if (xftaxnumber.Length > 16)
                                {
                                    string xftax = xftaxnumber.Substring(0, 16);
                                    foreach (BatchInfo bf in VendorscompanyAll)
                                    {
                                        if (bf.companyTaxnum.Length > 16)
                                        {
                                            string comtax = bf.companyTaxnum.Substring(0, 16);
                                            if (xftax == comtax)
                                            {
                                                iom.invoiceOcrSonLists[0].xfcompanyname = bf.companyName;
                                                iom.invoiceOcrSonLists[0].xfcompanynameInitialValue = bf.companyName;
                                                iom.invoiceOcrSonLists[0].xftaxnumber = bf.companyTaxnum;
                                                iom.invoiceOcrSonLists[0].xftaxnumberInitialValue = bf.companyTaxnum;
                                                break;
                                            }
                                        }
                                    }
                                }
                                else if (xftaxnumber.Length > 14)
                                {
                                    string xftaxmin = xftaxnumber.Substring(0, 14);
                                    foreach (BatchInfo bf in VendorscompanyAll)
                                    {
                                        if (bf.companyTaxnum.Length > 14)
                                        {
                                            string comtax = bf.companyTaxnum.Substring(0, 14);
                                            if (xftaxmin == comtax)
                                            {
                                                iom.invoiceOcrSonLists[0].xfcompanyname = bf.companyName;
                                                iom.invoiceOcrSonLists[0].xfcompanynameInitialValue = bf.companyName;
                                                iom.invoiceOcrSonLists[0].xftaxnumber = bf.companyTaxnum;
                                                iom.invoiceOcrSonLists[0].xftaxnumberInitialValue = bf.companyTaxnum;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                            string xfname = iom.invoiceOcrSonLists[0].xfcompanyname;
                            xfname = xfname.Replace('(', '（');
                            xfname = xfname.Replace(')', '）');
                            iom.invoiceOcrSonLists[0].xfcompanyname = xfname;
                            iom.invoiceOcrSonLists[0].xfcompanynameInitialValue = xfname;
                            iom.invoiceOcrSonLists[0].xfcompanynameState = CompanyOrMoneyStateEnum.InvoiceVendorsOrCustmIncorrect;
                        }
                    }
                }
                catch (Exception ex)
                {
                    WsdLogger.yxlog("error", "第" + tiaoNim + "条进行供应商或客户校验错误" + ex.Message);
                    continue;
                }
              
            }
        }
    }
}
